﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data;
using System.Threading;
using System.ComponentModel;
using System.Text;
using System.IO;
using Tab2Sql.Classes.db;
using Tab2Sql.Classes.Core;
using Tab2Sql.Classes;
using System.Drawing;
using System.Collections.Generic;
using NLog;

namespace Tab2Sql
{
    /// <summary>
    /// Main class for Winform.
    /// </summary>
    public partial class MainForm : Form
    {
        private static Logger logger = LogManager.GetCurrentClassLogger ();
        /// <summary>
        /// Default constructor. 
        /// </summary>
        public MainForm ()
        {
            logger.Trace ("Constructor called. ");
            InitializeComponent ();
            LoadRecentFileList ();
            MyOptions = new Options ();
            history = new historyDB ();
            Reset ();
            
        }
        //**********PUBLIC******************************************************************************
        /// <summary> Glocal datatable.</summary>
        public DataTable ourDataTable;


        //**********PRIVATE*******************************************************************************
        private MainTab2Sql mts;
        private historyDB history;
        private internalDB theInternalDB;
        private Options MyOptions;
        private Stopwatch swatch = new Stopwatch ();
        private bool largeFileMode = false;
        private long realLineCount = -1;

        // **********METHODS********************************************************************************

        /// <summary> //MAIN PROCESS START
        /// This is the main tab2sql button action. This kicks off process.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void tab2sql_click (object sender, EventArgs e)
        {
            logger.Trace ("tab2sql_click");

            //HACK : Hard-coded value. 
            // This should be moved to an XML file for more flexibility
            MyOptions.MaximumColumns = 10000;
            
            swatch.Start ();
            logger.Trace ("{0}: tab2sql_click. LaunchConversion about to start.",swatch.ElapsedMilliseconds);
            LaunchConversion ();
            logger.Trace ("{0}: tab2sql_click. LaunchConversion done.", swatch.ElapsedMilliseconds);
            DataTable statusTable = getStatusTable ();
            statusGrid.DataSource = statusTable;
            statusGrid.RowHeadersVisible = false;
            statusGrid.Refresh ();
            
        }

        private DataTable getStatusTable ()
        {
            DataTable statusTable = new DataTable ();
            try
            {
                statusTable.Columns.Add ("rowCount", typeof(Int32));
                statusTable.Columns.Add ("colCount", typeof(Int32));
                statusTable.Columns.Add ("timeToProcess", typeof(Int64));
                if (mts != null)
                {
                    object[] p = new object[3];
                    
                    p[0] = mts.OurDataTable.Rows.Count;
                    p[1] = mts.OurDataTable.Rows.Count;
                    p[2] = swatch.ElapsedMilliseconds;
                    statusTable.Rows.Add (p);
                    statusTable.AcceptChanges ();
                }
            } catch (Exception ex)
            {
                
                logger.Error (ex.Message);
                outStr.Text += Environment.NewLine + ex.Message;
            }
            
            return statusTable;
        }

        private void openFolderToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string pathName = GetFolderName ();
            if (Directory.Exists (pathName))
            {
                
                multiFiles mf = new multiFiles (pathName, ref this.MyOptions);
                mf.Go ();
                inStr.Text += Environment.NewLine + mf.OutputMessages;
                outStr.Text += Environment.NewLine + "Job completed.";
                outStr.Text += Environment.NewLine + "File size:" + mf.OutputBytes ();
                outStr.Text += Environment.NewLine + mf.OutputPath;
                mf = null;
            } else
            {
                outStr.Text += Environment.NewLine + "No/invalid directory supplied?";
            }
            
        }
        private void wipeTop_Click (object sender, EventArgs e)
        {
            inStr.Clear ();
            inStr.ClearUndo ();
        }
        private void save_Click (object sender, EventArgs e)
        {
            if (outStr.Text.Trim ().Length == 0)
            {
                showNoDataMessage ();
            } else
            {
                doSave ();
            }
        }
        private void saveFileDialog1_FileOk (object sender, CancelEventArgs e)
        {
            
            string fname = saveFileDialog1.FileName;
            string preamble = sqlFragments.getPreamble ();
            
            DGen.DFile.WriteToFile (fname, preamble + Environment.NewLine + outStr.Text);
            boxStatus.Text = fname;
        }
        private void quickSave_Click (object sender, EventArgs e)
        {
            boxStatus.Text = doQuickSave ();
        }
        private void exitToolStripMenuItem_Click (object sender, EventArgs e)
        {
            Reset ();
            Application.Exit ();
        }
        private void saveAsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            doSave ();
        }
        private void saveToolStripMenuItem_Click (object sender, EventArgs e)
        {
            boxStatus.Text = doQuickSave ();
        }
        private void wordStatsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            WordStats ws = new WordStats (inStr.Text);
            outStr.Text = ws.PrettifyStats ();
        }

        // COPY TO CLIPBOARD
        private void copy2clipboard_click (object sender, EventArgs e)
        {
            boxStatus.AppendText ("CLIPBOARD:" + Clipboard.GetText ().Length + " chars" + Environment.NewLine);
            if (outStr.Text.Length > 100 && outStr.Text.Length < 130000000)
            {
                Clipboard.SetText (outStr.Text);
            }
        }
        // COPY FROM CLIPBOARD, PROCESS AND THEN COPY TO CLIPBOARD
        private void btnFromClipboard_Click (object sender, EventArgs e)
        {
            inStr.Text = Clipboard.GetData ("Text").ToString ();
            if (LaunchConversion () == 0)
            {
                Clipboard.SetText (outStr.Text);
            }
        }
        // EXIT APPLICATION
        private void btnExit_Click (object sender, EventArgs e)
        {
            Application.Exit ();
        }
        private void cbUseFirstRowAsHeader_CheckedChanged (object sender, EventArgs e)
        {
            MyOptions.FirstRowIsHeader = cbUseFirstRowAsHeader.Checked;
        }
        // LOAD SINGLE FILE
        private void btnLoad (object sender, EventArgs e)
        {
            doLoadFile ();
        }
        // FILE IS SELECTED
        private void openFileDialog1_FileOk (object sender, System.ComponentModel.CancelEventArgs e)
        {
            //if (inStr.IsDone == true)
            //{
            //    return;
            //}
            //string fn = loadFileBox.FileName.ToLower();
            //if (fn.Contains(".csv") == true)
            //{
            //    startLoadProcess(fn);
            //}
            //else
            //{
            //    startLoadProcess(fn);
            //}
            //startLoadProcess(fn);
        }
        private void btnAbout_Click (object sender, EventArgs e)
        {
            AboutBox1 tmp = new AboutBox1 ();
            tmp.Show ();
        }
        /// <summary>
        /// Create Datatable 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createDT_Click (object sender, EventArgs e)
        {
            if (inStr.Text.Length < 20)
            {
                showNoDataMessage ();
            } else
            {
                MyOptions.OutputType = OutputTypes.DataTableOnly;
                bgw.RunWorkerAsync (new InputObject (inStr.Text, MyOptions));
                pivotButton.Enabled = true;
            }
        }
        private static void showNoDataMessage ()
        {
            MessageBox.Show ("Have you actually input anything? Try adding another few lines.", "ATTENTION : No data.");
        }
        /// <summary> //DATACONFIG FORM
        /// DataConfig
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        private void db_Click (object sender, EventArgs e)
        {
            Global.generatedSQL = outStr.Text;
            outStr.Clear ();
            DataConfig dc = new DataConfig ();
            dc.Show ();
            dc.Focus ();
        }
        /// <summary>  // SHOW PREVIEW FORM
        /// Preview form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showGrid_Click (object sender, EventArgs e)
        {
            // Previous button has been clicked. 
            if (inStr.Text.Length < 20 && dgv.Visible == false)
            {
                showNoDataMessage ();
                return;
            }
            if (inStr.Text.Length > 20 && dgv.Visible == true)
            {
                ShowGrid sg = new ShowGrid ((DataTable)dgv.DataSource);
                sg.Owner = this;
                sg.Show ();
                sg.Focus ();
            } else if (inStr.Text.Length > 20)
            {
                xTab2Sql x = new xTab2Sql (inStr.Text);
                x.Process ();
                
                ShowGrid sg = new ShowGrid (x);
                sg.Owner = this;
                sg.Show ();
                sg.Focus ();
            }
        }
        // MEM BUTTON IS PRESSED
        private void diagsBtn_Click (object sender, EventArgs e)
        {
            quickDiag ();
        }
        private void WipeBothClick (object sender, EventArgs e)
        {
            Reset ();
            Application.Restart ();
        }
        private void clearButton_Click (object sender, EventArgs e)
        {
            Reset ();
        }
        private void richtextCut_Click (object sender, EventArgs e)
        {
            if (inStr.Focused)
            {
                inStr.Cut ();
            } else if (outStr.Focused)
            {
                outStr.Cut ();
            } else if (boxStatus.Focused)
            {
                boxStatus.Cut ();
            } else
            {
                boxStatus.Text += "\nAmbiguous copy/cut command.";
            }
        }
        private void richtextCopy_Click (object sender, EventArgs e)
        {
            if (inStr.Focused)
            {
                inStr.Copy ();
            }
            if (outStr.Focused)
            {
                outStr.Copy ();
            } else if (boxStatus.Focused)
            {
                boxStatus.Copy ();
            } else
            {
                boxStatus.Text += "\nAmbiguous copy/cut command.";
            }
        }
        private void richtextPaste_Click (object sender, EventArgs e)
        {
            if (inStr.ContainsFocus)
            {
                inStr.Paste ();
            }
            if (outStr.ContainsFocus)
            {
                outStr.Paste ();
            }
        }
        private void selectAllToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.SelectAll ();
            if (outStr.ContainsFocus)
            {
                outStr.SelectAll ();
            }
        }
        private void radioButton2_CheckedChanged (object sender, EventArgs e)
        {
            if (singleInsert.Checked == true)
            {
                multiInsert.Checked = false;
            }
        }
        private void radioButton1_CheckedChanged (object sender, EventArgs e)
        {
            if (multiInsert.Checked == true)
            {
                singleInsert.Checked = false;
            }
        }

        private void pivotButton_Click (object sender, EventArgs e)
        {
            startPivot ();
        }
        private void checkBox2_CheckedChanged (object sender, EventArgs e)
        {
            if (cbAddCol1.Checked == true)
            {
                boxAddCol1.Enabled = true;
                lstAddCol1.Enabled = true;
                boxAddCol1.Text = "C1";
                boxAddCol1.Focus ();
            } else
            {
                boxAddCol1.Enabled = false;
                lstAddCol1.Enabled = false;
            }
        }
        private void ClearBottomTextBox_Click (object sender, EventArgs e)
        {
            outStr.Clear ();
        }
        /// <summary>
        /// Load text files. 
        /// </summary>
        /// <param name="sender">
        /// A <see cref="System.Object"/>
        /// </param>
        /// <param name="e">
        /// A <see cref="EventArgs"/>
        /// </param>
        private void openFileToolStripMenuItem_Click (object sender, EventArgs e)
        {
            doLoadFile ("txt");
        }

        private void OpenXLS (object sender, EventArgs e)
        {
            doLoadFile ("xls");
        }
        private void chooseDatabaseToolStripMenuItem_Click (object sender, EventArgs e)
        {
            doLoadFile ("sqlite");
        }
        private void input2SqliteToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (theInternalDB == null)
            {
                theInternalDB = new internalDB (inStr.Text, true);
            } else
            {
                theInternalDB.Process (inStr.Text);
            }
            outStr.Text = theInternalDB.Output.ToString ();
        }

        private void output2SqliteToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (theInternalDB == null)
            {
                theInternalDB = new internalDB (inStr.Text, true);
            } else
            {
                theInternalDB.Process (inStr.Text);
            }
            internalDB idb = new internalDB (outStr.Text.ToString ());
            
            outStr.Text = theInternalDB.Output.ToString ();
        }
        private void sqliteSchemaInfoToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (theInternalDB == null)
            {
                theInternalDB = new internalDB ();
                
            }
            dgv.DataSource = theInternalDB.Schema;
            dgv.ColumnHeadersBorderStyle = ProperColumnHeadersBorderStyle;
            dgv.RowHeadersVisible = false;
            dgv.Visible = true;
            dgv.AutoResizeColumns (DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
        }
        /// <summary>
        /// Remove the columnName header border in the Aero theme in Vista,
        /// but keep it for other themes such as standard and classic.
        /// </summary>
        static DataGridViewHeaderBorderStyle ProperColumnHeadersBorderStyle {
            get { return (System.Drawing.SystemFonts.MessageBoxFont.Name == "Segoe UI") ? DataGridViewHeaderBorderStyle.None : DataGridViewHeaderBorderStyle.Raised; }
        }
        //Detect delimiter
        private void AutoDetect_Click (object sender, EventArgs e)
        {
            if (inStr.Text.Length == 0)
            {
                boxStatus.Text += "No text.";
            } else
            {
                boxSeparator.Text = autoDelimiter;
            }
        }

        private void mySQLMSSQLToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string msSQL = "";
            if (outStr.Text.Length > 1)
            {
                MySql2SqlServer.ConvertTable (outStr.Text, ref msSQL);
                outStr.Text = msSQL;
            }
        }
        private void insertsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (outStr.Text.Length > 1)
            {
                outStr.Text = MySql2SqlServer.ConvertInsert (outStr.Text);
            }
        }
        private void includeSqlFooter_CheckedChanged (object sender, EventArgs e)
        {
            if (includeSqlFooter.CheckState == CheckState.Checked)
            {
                this.MyOptions.IncludeQueryFooter = true;
            }
            if (includeSqlFooter.CheckState == CheckState.Unchecked)
            {
                this.MyOptions.IncludeQueryFooter = false;
            }
        }
        private void sqlCommandFileToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (outStr.SelectedText.Length > 0)
            {
                string tmpName = outStr.SelectedText;
                if (File.Exists (tmpName))
                {
                    FileInfo finfo = new FileInfo (tmpName);
                    if (finfo.Length < 10000000)
                    {
                        outStr.Text = "-- " + tmpName + Environment.NewLine + File.ReadAllText (tmpName);
                    }
                }
            }
        }
        private void getFileListToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string pathName = GetFolderName ();
            inStr.Clear ();
            if (Directory.Exists (pathName))
            {
                string[] fileList = Directory.GetFiles (pathName);
                foreach (string fName in fileList)
                {
                    inStr.Text += Environment.NewLine;
                    inStr.Text += fName;
                }
            } else
            {
                outStr.Text += Environment.NewLine + "No/invalid directory supplied?";
            }
        }
        private void batchFilesIntoSingleTableToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string tmp = inStr.SelectedText;
            Combine combine = new Combine (tmp);
            if (combine.FCount > 0)
            {
                combine.Process ();
                outStr.Text = combine.FileLengths ();
                inStr.Clear ();
                inStr.Text = combine.CombinedFile.ToString ();
            }
        }
        private void inStrTextToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                xTab2Sql xts = new xTab2Sql (inStr.Text);
                DBAnalyse dba = new DBAnalyse (xts);
                outStr.Text += dba.AnalysisText ();
            } else
            {
                showNoDataMessage ();
            }
        }
        private void trimInputToolStripMenuItem_Click (object sender, EventArgs e)
        {
            boxStatus.Text += inStr.Lines.ToString () + "\t" + inStr.Text.Length;
            Regex r = new Regex ("[ ]{4,}");
            inStr.Text = r.Replace (inStr.Text, " ");
            boxStatus.Text += inStr.Lines.ToString () + "\t" + inStr.Text.Length;
        }
        private void reCount (object sender, EventArgs e)
        {
            reCount ();
        }

        private void reCount ()
        {
            int inLines = inStr.Lines.Length;
            lblTopRows.Text = string.Format ("{0:0,0} lines", inLines);
            if (inStr.IsTextAbridged == true)
            {
                lblTopRows.ForeColor = System.Drawing.Color.Red;
                lblTopRows.Text = string.Format ("{0:0,0} lines ({1:0,0} visible)", inStr.FullLineCount, inStr.VisibleLineCount);
 
            }

            lblTopRows.Visible = true;
        }
        private void runScriptFileToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string tmp = "";
            if (inStr.SelectionLength > 0)
            {
                tmp = inStr.Text.ToLower ();
                if (File.Exists (tmp) == true && tmp.EndsWith (".sql"))
                {
                    outStr.Text += "Starting..." + Environment.NewLine;
                    SqlCmd mySql = new SqlCmd (tmp);
                    outStr.Text += mySql.Stderror;
                    outStr.Text += "------------------------------" + Environment.NewLine;
                    outStr.Text += DGen.DText.Left (mySql.Stdout, 100000);
                }
            }
        }
        private void wordStatsToolStripMenuItem_Click_1 (object sender, EventArgs e)
        {
            var obj = (ToolStripItem)sender;
            string tmpTag = obj.Tag.ToString ();
            
            if (inStr.Text.Length < 5)
            {
                return;
            }
            //HACK : Hard coded value
            if (inStr.Text.Length > 1000000)
            {
                return;
            }
            Series nS;
            if (tmpTag == "paragraph")
            {
                nS = new Series (inStr.Text, true);
            } else
            {
                nS = new Series (inStr.Text);
            }
            
            outStr.AppendText (nS.finalMessage.ToString ());
            nS = null;
        }
        private void AlternativeMenus (object sender, KeyEventArgs e)
        {
            if (!e.Alt)
            {
                return;
            }
            if (clearButton.Text != "Archive")
            {
                clearButton.Text = "Archive";
                clearButton.Click -= new EventHandler (clearButton_Click);
                clearButton.Click += new EventHandler (clearButton_MouseClick);
            }
        }

        // This is the archive button (only visible when the user holds down the ALT key).
        void clearButton_MouseClick (object sender, EventArgs e)
        {
            if (inStr.TextLength < 40 || outStr.TextLength < 40)
            {
                return;
            }
            Archiver rkyv = new Archiver (inStr, outStr);
            rkyv.WriteXML ();
            rkyv = null;
            Reset ();
        }
        private void AlternativeMenusSwitch (object sender, KeyEventArgs e)
        {
            if (clearButton.Text != "Clear")
            {
                clearButton.Text = "Clear";
                clearButton.Click -= new EventHandler (clearButton_MouseClick);
                clearButton.Click += new EventHandler (clearButton_Click);
                //clearButton.Update();
            }
        }
        // Paste and Go...
        private void richtextPasteGo_Click (object sender, EventArgs e)
        {
            inStr.Paste ();
            outStr.Clear ();
            LaunchConversion ();
        }
        // Run SQL
        private void runSQL_Click (object sender, EventArgs e)
        {
            MessageBox.Show ("Not implemented.");
        }
        // Open HTML window and view current output.
        private void viewOutputAsHTMLNewWindowToolStripMenuItem_Click (object sender, EventArgs e)
        {
            MessageBox.Show ("Not implemented.");

            if (outStr.TextLength > 40 && 100 > 2000) //not a typo
            {
                var SqlD = new SqlDirect ();
                string tmp = SqlD.Prettify (outStr.Text);
                HTMLForm hForm = new HTMLForm ();
                hForm.TryThis (tmp);
                SqlD = null;
                hForm.Show ();
                hForm.Activate ();
            }
        }


        private void settingsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            var sPanel = new Tab2Sql.Forms.SettingsPanel ();
            sPanel.Show ();
            sPanel.Activate ();
        }
        //TODO : All of this.
        private void targetExistingTableToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                //string foo = "attcodes";
                //IntoExisting intoExist = new IntoExisting(foo);
                //outStr.Text += intoExist.InsertStatement;
                //inStr.Text += "First bit about to go"+Environment.NewLine;
                //SqlSmo ss = new SqlSmo(foo);
                //inStr.Text += "First bit done. Ready..." + Environment.NewLine;
                //if (ss.ConnectionWorking == true)
                //{
                //    ss.MainBit();
                //    inStr.Text += ss.EndResult.ToString();
                //}
            } else
            {
                showNoDataMessage ();
            }
        }

        private void dataFileToolStripMenuItem_Click (object sender, EventArgs e)
        {
            
            if (inStr.SelectedText.Length > 0)
            {
                
                string possibleFile = inStr.SelectedText.Trim ('"').Trim ();
                possibleFile = possibleFile.Trim ('\'');
                
                if (File.Exists (possibleFile))
                {
                    LoadFile lfile = new LoadFile (possibleFile, ref inStr);
                    realLineCount = lfile.LineCount;
                    largeFileMode = lfile.LargeFileMode;
                    return;
                }
            }
            doLoadFile ();
        }

        /*---------------------------------------------------------------------------------------------------------------------------------------------
         *  SQL CONVERT
         *  Most of these don't currently work. (15/11/2009)
         * ---------------------------------------------------------------------------------------------------------------------------------------------*/
        #region sqlConvertStuff

        private void tSqlToMSAccessToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                SqlConvert sc = new SqlConvert (SqlVariants.TSql, SqlVariants.Sqlite, inStr.Text);
                inStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
        }
        private void sqlAnywhereToTSqlToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                SqlConvert sc = new SqlConvert (SqlVariants.SqlAnywhere, SqlVariants.TSql, inStr.Text);
                sc.Translate ();
                outStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
        }
        private void sqlAnywhereToSqliteToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                SqlConvert sc = new SqlConvert (SqlVariants.SqlAnywhere, SqlVariants.Sqlite, inStr.Text);
                sc.Translate ();
                outStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
        }

        private void tSqlToInformixToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                SqlConvert sc = new SqlConvert (SqlVariants.TSql, SqlVariants.Informix, outStr.Text);
                sc.Translate ();
                outStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
            
        }
        private void tSqlToSqlAnywhereToolStripMenuItem_Click (object sender, EventArgs e)
        {
            
            if (inStr.TextLength > 5)
            {
                //HACK
                //TODO Inconsistent behaviour to sqlAnywhereToTSqlToolStripMenuItem_Click. 
                SqlConvert sc = new SqlConvert (SqlVariants.TSql, SqlVariants.SqlAnywhere, outStr.Text);
                sc.Translate ();
                outStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
        }
        private void tSqlToSQLiteToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                SqlConvert sc = new SqlConvert (SqlVariants.TSql, SqlVariants.Sqlite, outStr.Text);
                sc.Translate ();
                outStr.Text = sc.EndResult.ToString ();
            } else
            {
                showNoDataMessage ();
            }
        }
        #endregion



/*---------------------------------------------------------------------------------------------------------------------------------------------
         *  LIST2CSV
         *  Most of these don't currently work. (15/11/2009)
         * ---------------------------------------------------------------------------------------------------------------------------------------------*/        
        #region list2csv
                private void list2arrayToolStripMenuItem_Click (object sender, EventArgs e)
        {
            outStr.Text = List2Csv.List2Array (inStr.Text);
        }
        private void floatArrayToolStripMenuItem_Click (object sender, EventArgs e)
        {
            outStr.Text = List2Csv.List2Array (inStr.Text, DataTypes.numeric);
        }
        private void stringArrayToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.Text.Length < 20)
            {
                showNoDataMessage ();
            } else
            {
                outStr.Text = List2Csv.List2Array (inStr.Text);
            }
        }
        private void changeCaseMenuItem_Click (object sender, EventArgs e)

        {
            var obj = (ToolStripItem)sender;
            string tmpTag = obj.Tag.ToString ();
            CaseChangeRequested ccr = CaseChangeRequested.none;

            if (tmpTag == "tolower")
            {
                ccr = CaseChangeRequested.tolower;
            }


            if (tmpTag == "toupper")
            {
                ccr = CaseChangeRequested.toupper;
            }
            string[] t = inStr.Lines;

            InputAdjustment.AdjustCase (ref t, ccr);
            inStr.Lines = t;

            return;


        }

        private void distinctToolStripMenuItem_Click (object sender, EventArgs e)
        {
            
            if (inStr.Text.Length < 20)
            {
                showNoDataMessage ();
            } else
            {
                bool showStats = false;
                var obj = (ToolStripItem)sender;
                string tmpTag = obj.Tag.ToString ();
                if (tmpTag == "STATS")
                {
                    showStats = true;
                }
                
                if (swatch.IsRunning)
                {
                    swatch.Reset ();
                }
                swatch.Start ();
                outStr.Text = List2Csv.List2InStatement (inStr.Text, showStats, myListSortOption);
                
                boxStatus.AppendText (Environment.NewLine + string.Format ("\n{0} s", swatch.ElapsedMilliseconds / 1000));
            }
        }
        #endregion



        private void multipleUpdatesIntoOneToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                sqlTransform st = new sqlTransform (inStr.Text);
                outStr.Text = st.OutStatement;
            } else
            {
                showNoDataMessage ();
            }
        }


        private void listSToolStripMenuItem_Click (object sender, EventArgs e)
        {
            outStr.Text += SettingsHelper.ListSettings ();
        }



        void getRid (object sender, System.EventArgs e)
        {
            if (sender.GetType ().Name == "ToolStripMenuItem")
            {
                ToolStripMenuItem caller = (ToolStripMenuItem)sender;
                string myTag = caller.Tag.ToString ();
                inStr.Text = InputAdjustment.GetRidOf (myTag, inStr.Text);
                return;
            }
        }







        private void button3_Click (object sender, EventArgs e)
        {
            formatSql ();
        }
        private void formatStatementToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                formatSql ();
            } else
            {
                showNoDataMessage ();
            }
        }

        private void dateAdjust (object sender, EventArgs e)
        {
            ToolStripMenuItem caller = (ToolStripMenuItem)sender;
            string myTag = caller.Tag.ToString ();
            switch (myTag)
            {
            case "d.m.y-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_PERIODS, DateForms.DDMMYYYY_SLASHES);
                return;
            case "m/d/y-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.MMDDYYYY_SLASHES, DateForms.DDMMYYYY_SLASHES);
                return;
            case "d/m/y-m/d/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_SLASHES, DateForms.MMDDYYYY_SLASHES);
                return;
            case "d/m/y-y-m-d":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_SLASHES, DateForms.YYYYMMDD_HYPHENS);
                return;
            case "d-m-y-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_HYPHENS, DateForms.DDMMYYYY_SLASHES);
                return;
            case "d/m/y-unix":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_SLASHES, DateForms.UNIX_EPOCH_TIME);
                return;
            case "y-m-d-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.YYYYMMDD_HYPHENS, DateForms.DDMMYYYY_SLASHES);
                return;
            case "m.d.y-m/d/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.MMDDYYYY_PERIODS, DateForms.MMDDYYYY_SLASHES);
                return;
            case "m-d-y-m/d/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.MMDDYYYY_HYPHENS, DateForms.MMDDYYYY_SLASHES);
                return;
            case "y.m.d-y-m-d":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.YYYYMMDD_PERIODS, DateForms.YYYYMMDD_HYPHENS);
                return;
            case "d mm y-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDmmmmYYYY_SPACES, DateForms.DDMMYYYY_SLASHES);
                return;
            case "dmy-d/m/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.DDMMYYYY_NONE, DateForms.DDMMYYYY_SLASHES);
                return;
            case "ymd-y-m-d":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.YYYYMMDD_NONE, DateForms.YYYYMMDD_HYPHENS);
                return;
            case "mdy-m/d/y":
                inStr.Text = InputAdjustment.DateConvert (inStr.Text, DateForms.MMDDYYYY_NONE, DateForms.MMDDYYYY_SLASHES);
                return;
            default:
                break;
            }
            
        }
        private void showOnlyXDelimitersToolStripMenuItem_Click (object sender, EventArgs e)
        {
            FilterByDelimiter ();
        }

        private void delimiterScanToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                outStr.Text = delimiterScan (inStr.Lines);
            } else
            {
                showNoDataMessage ();
            }
        }
        private void tabToPipeToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace ("\t", "|");
        }
        private void pipeToTabToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace ("|", "\t");
        }
        private void spaceToPipeToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string tmpText = inStr.Text;
            
            tmpText = tmpText.Replace (@"\n", "xxx7714332xxx");
            tmpText = tmpText.Replace (" ", "|");
            tmpText = tmpText.Replace ("xxx7714332xxx", "\n");
            inStr.Text = tmpText;
            
        }
        private void pipeToCommaToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace ("|", ",");
        }
        private void commaToPipeToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace (",", "|");
        }
        private void commaToTabToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace (",", "\t");
        }
        private void tabToCommaToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Text = inStr.Text.Replace ("\t", ",");
        }
        private void dataOutputToolStripMenuItem_Click (object sender, EventArgs e)
        {
            
            MyOptions.OutputType = OutputTypes.DataOutput;
            // TSV2DataTable tsv = new TSV2DataTable(inStr.Text, MyOptions, MyObject);
            //ourDataTable = tsv.Dt;
            //DataTable2SQL dts = new DataTable2SQL(ourDataTable, SqlStatementType.DataMode, MyOptions);
            //outStr.Rtf = dts.theRtb.Rtf;
        }
        private void fixedWidthDisplayToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                inStr.SelectAll ();
                inStr.SelectionFont = new System.Drawing.Font ("Courier New", 9.0f);
                inStr.Refresh ();
                inStr.Select (0, 0);
                if (inStr.Lines[0].Trim ().Length == 0)
                {
                    inStr.Text.Replace ("\n\n", "\n");
                }
            } else
            {
                showNoDataMessage ();
            }
        }

        //HACK : Remove this at some point. Test use only (note made 31/10/2009)

        private void displayFixedWidthInput (object sender, EventArgs e)
        {
            if (inStr.TextLength > 5)
            {
                inStr.SelectAll ();
                inStr.SelectionFont = new System.Drawing.Font ("Courier New", 9.0f);
                inStr.Refresh ();
                inStr.Select (0, 0);
                if (inStr.Lines[0].Trim ().Length == 0)
                {
                    inStr.Text.Replace ("\n\n", "\n");
                }
            } else
            {
                showNoDataMessage ();
            }
        }
        private void displayOutputInFixedWidthFontToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (outStr.TextLength > 5)
            {
                outStr.SelectAll ();
                outStr.SelectionFont = new System.Drawing.Font ("Courier New", 8.5f);
                outStr.Refresh ();
                outStr.Select (0, 0);
                if (outStr.Lines[0].Trim ().Length == 0)
                {
                    outStr.Text.Replace ("\n\n", "\n");
                }
            }
        }
        private void toolStripMenuItem8_Click (object sender, EventArgs e)
        {
            outStr.ZoomFactor = 0.8f;
        }
        private void toolStripMenuItem9_Click (object sender, EventArgs e)
        {
            outStr.ZoomFactor = 1.0f;
        }
        private void toolStripMenuItem10_Click (object sender, EventArgs e)
        {
            outStr.ZoomFactor = 1.2f;
        }

        private void updateDelimiter (object sender, EventArgs e)
        {
            if (MyOptions.Delimiter == boxSeparator.Text)
            {
            } else
            {
                if (boxSeparator.Text.Length > 0)
                {
                    MyOptions.Delimiter = boxSeparator.Text;
                }
            }
            
        }

        private void button1_Click (object sender, EventArgs e)
        {
            int h = statusGrid.Size.Height;
            int w = statusGrid.Size.Width;
            int x = statusGrid.Location.X;
            int y = statusGrid.Location.Y;
            if (inStr.Visible)
            {
                inStr.Visible = false;
                
                statusGrid.Size = new Size (w, h + 200);
                statusGrid.Location = new Point (x, y - 200);
                statusGrid.Visible = true;
            } else
            {
                statusGrid.Visible = false;
                statusGrid.Size = new Size (w, h - 200);
                statusGrid.Location = new Point (x, y + 200);
                
                inStr.Visible = true;
            }
            statusGrid.Refresh ();
            
        }

        private void boxChosenTableName_TextChanged (object sender, EventArgs e)
        {
            if (MyOptions.TableName != boxChosenTableName.Text)
            {
                MyOptions.TableName = boxChosenTableName.Text;
            }
        }

        private void backgroundWorker1_DoWork (object sender, DoWorkEventArgs e)
        {
            
        }

        private void otherLaunch ()
        {
            if (swatch.IsRunning == false)
            {
                swatch.Start ();
            }
            logger.Trace ("{0}: otherLaunch started.", swatch.ElapsedMilliseconds);

            outStr.Clear ();
            if (MyOptions.AutoDetectDelimiter == true)
            {
                MyOptions.Delimiter = autoDelimiter;
            }
            logger.Trace ("{0}: bgw.RunWorkerAsync imminent. ", swatch.ElapsedMilliseconds);
            bgw.RunWorkerAsync (new InputObject (inStr.Text, MyOptions));
            logger.Trace ("{0}: bgw.RunWorkerAsync completed. ", swatch.ElapsedMilliseconds);
        }

        private void bgw_DoWork (object sender, DoWorkEventArgs e)
        {
            logger.Trace ("{0}: bgw_DoWork Started. ", swatch.ElapsedMilliseconds);

            InputObject io = e.Argument as InputObject;
            
            MainTab2Sql oMts = new MainTab2Sql (io);
            bgw.ReportProgress (10);
            logger.Trace ("{0}: bgw_DoWork - 10% Report Stage. ", swatch.ElapsedMilliseconds);
            oMts.Go ();
            bgw.ReportProgress (50);
            logger.Trace ("{0}: bgw_DoWork - 50% Report Stage. ", swatch.ElapsedMilliseconds);
            string foo = String.Empty;
            if (io.TheOptions.OutputType == OutputTypes.JustCreateStatement)
            {
                foo = oMts.CreateTable;
            } else if (io.TheOptions.OutputType == OutputTypes.DataTableOnly)
            {
                mts = oMts;
                foo = "Done";
            } else
            {
                logger.Trace ("{0}: bgw_DoWork - PRE  - foo = oMts.FullSql Stage. ", swatch.ElapsedMilliseconds);
                foo = oMts.FullSql;
                logger.Trace ("{0}: bgw_DoWork - POST - foo = oMts.FullSql Stage. foo is {1} chars long. ", swatch.ElapsedMilliseconds, foo.Length);

            }
            bgw.ReportProgress (100);
            e.Result = foo;
        }


        private void bgw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
        {

            logger.Trace ("{0}: bgw_RunWorkerCompleted- string foo = e.Result.ToString () Stage. ", swatch.ElapsedMilliseconds);
            string foo = e.Result.ToString ();

            logger.Trace ("{0}: bgw_RunWorkerCompleted- ABOUT TO UPDATE GUI. ", swatch.ElapsedMilliseconds);
            outStr.Text = foo;

        }

        private void bgw_ProgressChanged (object sender, ProgressChangedEventArgs e)
        {
            
            int p = 0;
            try
            {
                p = e.ProgressPercentage;
            } catch (Exception ex)
            {
                logger.Error (ex.Message);
                
            }
            if (p != 0)
            {
                boxStatus.AppendText (string.Format ("\nWORKING {0} / {1}", p, swatch.ElapsedMilliseconds));
            }
        }

        private void sortElementsByLengthToolStripMenuItem_CheckStateChanged (object sender, EventArgs e)
        {
            
        }


        List2CSVSortBy myListSortOption = List2CSVSortBy.Name;
        private void changeListSort (object sender, EventArgs e)
        {
            var x = (ToolStripItem)sender;
            if (x.Tag.ToString () == "SORTBYNAME")
            {
                myListSortOption = List2CSVSortBy.Name;
                sortElementsByLength.Checked = false;
                doNotSortElements.Checked = false;
                sortElementsByName.Checked = true;
                sortElementsByDate.Checked = false;
            } else if (x.Tag.ToString () == "SORTBYLENGTH")
            {
                myListSortOption = List2CSVSortBy.Length;
                sortElementsByLength.Checked = true;
                doNotSortElements.Checked = false;
                sortElementsByName.Checked = false;
                sortElementsByDate.Checked = false;
            } else if (x.Tag.ToString () == "SORTBYNONE")
            {
                myListSortOption = List2CSVSortBy.None;
                sortElementsByLength.Checked = false;
                doNotSortElements.Checked = true;
                
                sortElementsByName.Checked = false;
                sortElementsByDate.Checked = false;
            } else if (x.Tag.ToString () == "SORTBYDATE")
            {
                myListSortOption = List2CSVSortBy.ValueDate;
                sortElementsByLength.Checked = false;
                doNotSortElements.Checked = false;
                sortElementsByName.Checked = false;
                sortElementsByDate.Checked = true;
            } else
            {
                myListSortOption = List2CSVSortBy.Name;
                sortElementsByLength.Checked = false;
                doNotSortElements.Checked = false;
                sortElementsByName.Checked = true;
                sortElementsByDate.Checked = false;
            }
        }

        private void addFileEntryToolStripMenuItem_Click (object sender, EventArgs e)
        {
            
            string fileName = "moo";
            if (inStr.SelectedText.Length > 0)
            {
                fileName = inStr.SelectedText;
            }
            if (history == null)
            {
                history = new historyDB ();
            }
            
            int res = history.AddFileEntry (fileName);
            if (res > 0)
            {
                outStr.Text += string.Format ("Success! {0} record(s) added.\n", res);
            } else
            {
                outStr.Text += string.Format ("Failure to add record. Status Code {0}\n ", res);
            }
            
            outStr.Text += history.ReadFileEntries ();
            outStr.Text += "\n" + history.LastStatement;
        }


        private void LoadRecentFileList ()
        {
            try
            {
                this.recentToolStripMenuItem.Enabled = true;
                this.recentToolStripMenuItem.DropDownItems.Clear ();
                this.recentToolStripMenuItem.DropDownItems.AddRange (RecentFiles ());
                this.recentToolStripMenuItem.GetCurrentParent ().Refresh ();
            } catch (Exception ex)
            {
                logger.Error ("MainForm.LoadRecentFileList caused an error. Message : {0}", ex.Message);
            }
            
        }
        private ToolStripMenuItem[] RecentFiles ()
        {
            historyDB history = new historyDB ();
            
            
            List<string> fileList = history.ReadFileEntryList ();
            int nFiles = fileList.Count;
            if (nFiles > 10)
            {
                nFiles = 10;
            }
            ToolStripMenuItem singleEntry;
            ToolStripMenuItem[] endResult = new ToolStripMenuItem[nFiles];
            for (int i = 0; i < nFiles; i++)
            {
                singleEntry = new ToolStripMenuItem (fileList[i]);
                singleEntry.Click += new EventHandler (singleEntry_Click);
                
                endResult[i] = singleEntry;
                
            }
            return endResult;
        }

        void singleEntry_Click (object sender, EventArgs e)
        {
            ToolStripMenuItem tmp = (ToolStripMenuItem)sender;
            string fileName = tmp.Text;
            if (System.IO.File.Exists (fileName))
            {
                LoadFile LFile = new LoadFile (fileName, ref inStr);
                realLineCount = LFile.LineCount;
                largeFileMode = LFile.LargeFileMode;
            }
            
            
            
        }
        /*---------------------------------------------------------------------------------------------------------------------------------------------
        *  SQL EDITOR MODE
        *  For working with SQLite / built-in DB.
        * ---------------------------------------------------------------------------------------------------------------------------------------------*/
        private void adhocToolStripMenuItem_Click (object sender, EventArgs e)
        {
            string statement = "";
            statement = outStr.Text.ToString ();
            if (inStr.SelectedText.Length > 10)
            {
                statement = inStr.SelectedText;
            } else if (outStr.SelectedText.Length > 10)
            {
                statement = outStr.SelectedText;
            } else if (outStr.TextLength * inStr.TextLength > 0 && inStr.SelectedText.Length < 10 && outStr.SelectedText.Length > 10)
            {
                statement = outStr.Text;
            } else if (outStr.TextLength * inStr.TextLength == 0 && outStr.TextLength > 0)
            {
                statement = outStr.Text;
            } else if (outStr.TextLength * inStr.TextLength == 0 && inStr.TextLength > 0 && outStr.TextLength < 10)
            {
                statement = inStr.Text;
            }
            if (statement.Length == 0)
            {
                
                statement = "SELECT 'Fail" + inStr.TextLength.ToString () + "-" + outStr.TextLength.ToString () + "'";
            }
            string response = history.Adhoc (statement);
            if (response == "CLS")
            {
                inStr.Clear ();
                //outStr.Clear ();
            } else
            {
                //outStr.Clear ();
                outStr.Text += response;
            }
        }
        private void nextToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (history != null)
            {
                inStr.Text = history.StatementLoad (historyDB.Direction.Next);
                updateRecordIndicator ();
            }
        }

        private void updateRecordIndicator ()
        {
            boxStatus.AppendText (string.Format ("{0} / {1}\n", history.InternalCounter, history.CommandLogSize));
        }
        private void previousToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (history != null)
            {
                inStr.Text = history.StatementLoad (historyDB.Direction.Previous);
                updateRecordIndicator ();
            }
            
        }

        private void justHeaderToolStripMenuItem_Click (object sender, EventArgs e)
        {
            MyOptions.OutputType = OutputTypes.JustCreateStatement;
            if (swatch.IsRunning == false)
            {
                swatch.Start ();
            }
            outStr.Clear ();
            if (MyOptions.AutoDetectDelimiter == true)
            {
                MyOptions.Delimiter = autoDelimiter;
            }
            
            bgw.RunWorkerAsync (new InputObject (inStr.Text, MyOptions));
        }

        private void bothToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Clear ();
            outStr.Clear ();
        }

        private void topToolStripMenuItem_Click (object sender, EventArgs e)
        {
            inStr.Clear ();
        }

        private void bottomToolStripMenuItem_Click (object sender, EventArgs e)
        {
            outStr.Clear ();
        }

        private void RemoveLines (object sender, EventArgs e)
        {
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            string s = t.Tag.ToString ();
            
            int LinesToKeep = 0;
            int LinesBefore = inStr.Lines.Count ();
            
            if (s == "comments")
            {
                string[] adjustedInStr = new string[LinesBefore];
                
                
                
                int newLineCounter = 0;
                foreach (var item in inStr.Lines)
                {
                    if (!item.ToString ().StartsWith ("#"))
                    {
                        adjustedInStr[newLineCounter] = item;
                        newLineCounter++;
                    }
                }
                
                
                string[] adjustedInStrTrim = (string[])DGen.DGen.ResizeArray (adjustedInStr, newLineCounter);
                
                inStr.Lines = adjustedInStrTrim;
                return;
                
                
            } else if (s == "90%")
            {
                double tmpL = (double)(LinesBefore * 0.1);
                LinesToKeep = Convert.ToInt32 (tmpL);
            } else if (s == "50%")
            {
                double tmpL2 = (double)(LinesBefore * 0.5);
                LinesToKeep = Convert.ToInt32 (tmpL2);
            } else
            {
                int tmpI = 0;
                bool tmpRes = Int32.TryParse (s, out tmpI);
                if (tmpRes == false)
                {
                    tmpI = LinesBefore;
                }
                LinesToKeep = Math.Min (tmpI, LinesBefore);
            }
            if (LinesBefore > LinesToKeep)
            {
                string[] newInStr = new string[LinesToKeep];
                Array.Copy (inStr.Lines, 0, newInStr, 0, LinesToKeep);
                
                inStr.Lines = newInStr;
            } else
            {
                boxStatus.AppendText ("InStr's contents below the number of rows requested. No action taken.");
            }
        }

        private void LimitedColumns (object sender, EventArgs e)
        {
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            string tag = t.Tag.ToString ();
            int tmpI = 0;
            bool tmpRes = Int32.TryParse (tag, out tmpI);
            if (tmpRes == true)
            {
                MyOptions.MaximumColumns = tmpI;
                if (swatch.IsRunning == false)
                {
                    swatch.Start ();
                }
                outStr.Clear ();
                if (MyOptions.AutoDetectDelimiter == true)
                {
                    MyOptions.Delimiter = autoDelimiter;
                }
                
                bgw.RunWorkerAsync (new InputObject (inStr.Text, MyOptions));
            }
        }

        private void basicSqliteInfoToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (theInternalDB == null)
            {
                theInternalDB = new internalDB ();
                
            }
            inStr.Text += theInternalDB.BasicInfo;
            inStr.Text += theInternalDB.SchemaInfo;
        }

        private void outputStatementToSqliteToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (theInternalDB == null)
            {
                theInternalDB = new internalDB ();
                
            }
            
            theInternalDB.Process (outStr.Text);
            boxStatus.Clear ();
            boxStatus.AppendText (theInternalDB.Output.ToString ());
        }

        private void matchUPRNToolStripMenuItem_Click (object sender, EventArgs e)
        {
            Tab2Sql.Forms.GarbageIn gigo = new Tab2Sql.Forms.GarbageIn ();
            if (inStr.TextLength > 5)
            {
                gigo.MatchUPRN (inStr.Lines);
                outStr.Text = gigo.Output.ToString ();
            }
            
        }

        private void matchAddressToolStripMenuItem_Click (object sender, EventArgs e)
        {
            Tab2Sql.Forms.GarbageIn gigo = new Tab2Sql.Forms.GarbageIn ();
            if (inStr.TextLength > 5)
            {
                gigo.MatchAddress (inStr.Lines);
                outStr.Text = gigo.Output.ToString ();
            }
        }
        private void matchAddressFuzzyToolStripMenuItem_Click (object sender, EventArgs e)
        {
            Tab2Sql.Forms.GarbageIn gigo = new Tab2Sql.Forms.GarbageIn ();
            if (inStr.TextLength > 5)
            {
                gigo.MatchImperfect (inStr.Lines);
                outStr.Text = gigo.Output.ToString ();
            }
        }
        private void distinctLinesOnlyToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength > 10)
            {
                List<string> InputStr = new List<string> (inStr.Lines.Count ());
                foreach (string s in inStr.Lines)
                {
                    if (!InputStr.Contains (s))
                    {
                        InputStr.Add (s);
                    }
                }
                inStr.Clear ();
                inStr.Lines = InputStr.ToArray<string> () as string[];
            }
            
        }
        private void sortInputMenuItem_Click (object sender, EventArgs e)
        {
            var txt = inStr.Lines;
            InputAdjustment.SortInputText (ref txt);
            inStr.ResetText();

            inStr.Lines = txt;
            
        }

        private void splitInputMenuItem_Click (object sender, EventArgs e)
        {
            var s = new System.Diagnostics.Stopwatch ();
            s.Start ();
            logger.Trace ("{0}: And we're off. splitInputMenuItem_Click is active.", s.ElapsedMilliseconds);
            
            char[] delimiters = new char[] { '\r', '\n', ',', '.', '-', ' ' };
            
            logger.Trace ("{0}: First split starting....", s.ElapsedMilliseconds);
            
            string[] mySeries = inStr.Text.Split (delimiters, StringSplitOptions.RemoveEmptyEntries);
            inStr.Clear ();
            int i = 0;
            StringBuilder sb = new StringBuilder (mySeries.Length * 15);



            foreach (string item in mySeries)

            {
                sb.AppendLine (item);

                i++;
                
                if (i % 50000 == 0)
                {
                    logger.Trace ("{0}: Instring loop. Line Number {1}.", s.ElapsedMilliseconds, i);
                }
            




            }
            logger.Trace ("{0}: And we are ALMOST done.", s.ElapsedMilliseconds);
            string tPath = DGen.DFile.uniqueFileName ();
            DGen.DFile.WriteToFile (tPath, sb.ToString ());
            boxStatus.AppendText (tPath + " file created.");

            inStr.AppendText (ref sb);

            logger.Trace ("{0}: And we are done.", s.ElapsedMilliseconds);

        }

        private void includeRowNumber_CheckedChanged (object sender, EventArgs e)
        {
            if (includeRowNumber.Checked == true)
            {
                MyOptions.IncludeRowNumber = RowID.AddRowNumber;
            }
            if (!includeRowNumber.Checked)
            {
                MyOptions.IncludeRowNumber = RowID.None;
            }
        }

        private void basicStatsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            if (inStr.TextLength < 10)
            {
                showNoDataMessage();
                return;
            }
            StringBuilder msg = new StringBuilder ();
            msg.AppendFormat ("\nLines : {0}", inStr.Lines.Count ());
            msg.AppendFormat ("\nChars : {0}", inStr.TextLength);
            msg.AppendFormat ("\nColumns : {0}", DGen.DText.CountOccurences (MyOptions.Delimiter, inStr.Text) / inStr.Lines.Count ());
            MessageBox.Show (msg.ToString ());
        }

        private void unlimitedColumnsToolStripMenuItem_Click (object sender, EventArgs e)
        {
            //HACK : Hard-coded value.
            MyOptions.MaximumColumns = 10000;
            swatch.Start ();
            LaunchConversion ();
        }
        
        
    }
}
